/**
 * @class NG.views.WorkspaceList
 * @extends Ext.Panel
 * 
 * Registered workspaces are listed on this panel. 
 * You should render map of workspace by tapping on this list.
 */
NG.views.WorkspaceList = Ext.extend(Ext.Panel, {
	iconCls: 'bookmarks',
	title: L_WSS,
    dockedItems: [{
        xtype: 'toolbar',
        title: L_WSS,
        
        items:[{
        	text: L_BACK,
        	ui:'back',
        	handler:function(){
	 			Ext.dispatch({
	                controller: app.controllers.workspaceList,
	                action: 'back'
	            });        	
        	}        	
		},
		{xtype:'spacer'},
		{
			text: L_NEW_WS,
            ui: 'confirm',               
			iconMask: true,
			handler:function(){
				Ext.dispatch({
					controller: app.controllers.index,
					action: 'newWorkspace',
					caller: app.views.workspaceList.getId(),
					animation: {type:'slide', direction:'right'}
				});			
			}
		}]
    }]	
    ,items: [{
    	id:'netgis-workspace-list',
        xtype: 'list',
		store: NG.stores.workspace,
		itemTpl: '{name}',
		tpl: 'aaa{name}bbb',
		singleSelect: true,
        onItemDisclosure: function (record, btn, index) {
            Ext.dispatch({
                controller: app.controllers.workspaceList
                ,action: 'showMap'
                ,id: record.getId()
            });
        },
        listeners: {
        	itemswipe: function(list, index, item, e) {
				e.preventDefault();
				var discItem = Ext.DomQuery.selectNode("[class=x-list-item-body]",item);
				var deleteDivId = "delete-" + index;
				var updateDivId = "update-" + index;
				Ext.DomHelper.append(discItem,"<table><tr><td><div id='" + deleteDivId + "'></div></td><td style='padding-left: 20px;'><div id='"  + updateDivId + "'></div></td></tr></table>");
				if (Ext.DomQuery.selectNode("[class*=netgis-delete-button]",item))
					return;
				var record = list.getStore().getAt(index);
	            var buttonSil = new Ext.Button({
	                text:		L_REMOVE,
	                ui:			"decline",
	                cls:		"netgis-delete-button",
	                width:		50,
	                renderTo:	deleteDivId,
	                handler:	function(btn, e) {
                	    e.preventDefault();
						Ext.dispatch({
			                controller: app.controllers.workspaceList,
			                action: 'deleteWorkspace',
			                caller: app.views.workspaceList.getId(),
			                id: record.getId()
			            });	                	
	                }
	            });
	            var buttonDegistir = new Ext.Button({
	                text:		L_UPDATE,
	                ui:			"confirm",
	                cls:		"netgis-edit-button",
	                width:		100,
	                renderTo:	updateDivId,
	                handler:	function(btn, e) {
                	    e.preventDefault();
                	    NGLogger.info('değiştir ' + record.getId());
                		list.deselect(list.getSelectedRecords(), false);
						Ext.dispatch({
			                controller: app.controllers.workspaceForm,
			                action: 'show',
			                caller: app.views.workspaceList.getId(),
			                id: record.getId()
			            });	                	
	                }
	            });
        	},

        	/**
        	 * show related map of workspace
        	 * 
        	 * @param {} dataView
        	 * @param {} index
        	 * @param {} item
        	 * @param {} e
        	 */
        	itemtap : function(list, index, item, e ) {
			}
        }         
        //,grouped: true
    }]
    ,listeners: {
    	activate : function (comp) {
    		NG.stores.workspace.load();
    		var list = Ext.getCmp('netgis-workspace-list');
    		list.refresh();
    		list.deselect(list.getSelectedRecords(), false);
    	}  
    }
    ,initComponent: function() {
		NGLogger.info('workspace list init');
        NG.views.WorkspaceList.superclass.initComponent.apply(this, arguments);
    }
});